package com.df.excel_export.validator.vadator_impl.type_dbarray;

import com.df.excel_export.common.ExcelTable;
import com.df.excel_export.validator.AbstractIFieldValidator;
import com.df.excel_export.validator.IFieldValidator;
import com.df.excel_export.validator.IValidatorFactory;
import com.df.excel_export.validator.ValidatorManager;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DbArrayRuleValidator extends AbstractIFieldValidator implements IValidatorFactory {

    public Set<Integer> outerIndex = new HashSet<>();//二维数组外部元素索引
    public Set<Integer> innerIndex = new HashSet<>();//二维数组内部元素索引
    public IFieldValidator validator;

    public String elementType;


    @Override
    public boolean support(String filedType, String ruleStr) {
        String regex = "^(int|long|float|string)\\[\\]\\[\\]$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(filedType);
        if(!matcher.find()) {
            return false;
        }

        String regexRule = "^\\[*?\\]\\[*?\\]\\(*?\\)$";
        Pattern patternRule = Pattern.compile(regexRule);
        Matcher matcherRule = patternRule.matcher(ruleStr);
        if(matcherRule.find()) {
            return true;
        }
        return false;
    }

    @Override
    public IFieldValidator create(String filedType, String ruleStr) {
        DbArrayRuleValidator validator = new DbArrayRuleValidator();

        //规则表达式
        String regexRule = "^\\[(.*?)\\]\\[(.*?)\\]\\((.*)\\)$";
        Pattern patternRule = Pattern.compile(regexRule);
        Matcher matcherRule = patternRule.matcher(ruleStr);
        String outerIndexStr = null;
        String innterIndexStr = null;

        String elemtnRuleStr = null;
        if(matcherRule.find()) {
            outerIndexStr = matcherRule.group(1);
            innterIndexStr = matcherRule.group(2);
            elemtnRuleStr = matcherRule.group(3);
        }
        //解析索引
        //外部元素索引
        String[] split = outerIndexStr.split(",");
        for (String s : split) {
            List<Integer> list = ValidatorManager.parseElementIndex(s);
            if(list != null && !list.isEmpty()) {
                validator.outerIndex.addAll(list);
            }
        }
        //内部元素
        if(innterIndexStr.equals("-")){
            innerIndex = null;
            //元素数据类型
            String regex = "^(int|long|float|string\\[\\])\\[\\]$";
            String type = parseElementType(regex, filedType);
            validator.elementType = type;
            //解析规则,此处为内层  一维数组的规则
            ValidatorManager.parseValidator(validator.elementType, elemtnRuleStr);
        }else{
            //元素数据类型
            String regex = "^(int|long|float|string)\\[\\]\\[\\]$";
            String type = parseElementType(regex, filedType);
            validator.elementType = type;
            String[] split2 = innterIndexStr.split(",");
            for (String s : split2) {
                List<Integer> list = ValidatorManager.parseElementIndex(s);
                if(list != null && !list.isEmpty()) {
                    validator.innerIndex.addAll(list);
                }
            }
            //解析规则，此处为内层元素的规则
            ValidatorManager.parseValidator(validator.elementType, elemtnRuleStr);
        }



        return validator;
    }

    @Override
    public Integer getPriority() {
        return 0;
    }

    @Override
    public boolean validate(ExcelTable checkTable, List<Object> rowData, Integer dataRowIndex, Integer fieldIndex, String fieldName, String fieldTypeStr, Object filedValue) {
        List<List<?>> value = (List<List<?>>) filedValue;
        for (Integer oIndex : outerIndex) {
            List<?> interArray = value.get(oIndex);
            if(innerIndex ==null){//内层一维数组的规则
                if(!validator.validate(checkTable, rowData, dataRowIndex, fieldIndex, fieldName, elementType, interArray)){
                    return false;
                }
            }else{//内层一维数组元素的规则
                for (Integer i : innerIndex) {
                    Object ArrayIndexValue = value.get(i);
                    if(!validator.validate(checkTable, rowData, dataRowIndex, fieldIndex, fieldName, elementType, ArrayIndexValue)){
                        //元素校验失败：文件，表名，行，列，元素索引，元素值，规则表达式
                        return false;
                    }
                }
            }
        }


        return true;
    }

    public String parseElementType(String regex,String filedTypeStr){

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(filedTypeStr);
        if(!matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }

    public static void main(String[] args) {
        String ruleStr1 = "[1,2,4,gt(5)](min(0))";
        String ruleStr = "[range(1,3)](eps(1+2+3))";
        String ruleStr2 = "[1,2,3](min(0))";
        String regexRule = "^\\[(.*?)\\]\\((.*)\\)$";
        Pattern patternRule = Pattern.compile(regexRule);
        Matcher matcherRule = patternRule.matcher(ruleStr);
        if(matcherRule.find()) {
            System.out.println("匹配成功！");
            String group1 = matcherRule.group(1);
            System.out.println("匹配成功:"+group1);
            String group2 = matcherRule.group(2);
            System.out.println("匹配成功:"+group2);
        }
    }

}
